<?php
class Mbook extends CI_Model{
    
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function getBook(){
        $this->db->select(' book.id as book_id, book.name as book_name, book.uname as book_uname, page_title, page_keyword, 
                            page_description, description, book.type as book_type, use_volume, status
                         ');
        $this->db->from('book');
        $this->db->order_by('book.id', 'desc');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function getBookByLimit($number, $offset){
        $this->db->select(' book.id as book_id, book.name as book_name, book.uname as book_uname, book.image_id as book_image_id,
                            book.page_title as book_page_title, book.page_keyword as book_page_keyword, 
                            book.page_description as book_page_description, book.type as book_type, use_volume, status,
                            author.name as author_name, filename, category.name as category_name
                         ');
        $this->db->from('book');
        $this->db->join('category_book'        , 'category_book.book_id = book.id', 'inner');
        $this->db->join('category'        , 'category_book.category_id = category.id', 'inner');
        $this->db->join('author'        , 'book.author_id = author.id', 'inner');
        $this->db->join('image'        , 'book.image_id = image.id', 'left');
        $this->db->order_by('book.id', 'desc'); 
        $this->db->limit($number, $offset);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function insertBook($param){
        $data = array(
            'id' => NULL,
            'name' => $param['name'],
            'uname' => $param['uname'],
            'open' => 'Y',
            'image_id' => $param['image_id'],
            'page_title' => $param['page_title'],
            'page_keyword' => $param['page_keyword'],
            'page_description' => $param['page_description'],
            'description' => $param['description'],
            'mob_description' => $param['mob_description'],
            'author_id' => $param['author_id'],
            'type' => $param['type'],
            'use_volume' => $param['use_volume'],
            'status' => $param['status']
        );
        $this->db->insert('book', $data);
        return $this->db->insert_id();
    }

    public function updateBook($param){
        $data = array(
            'name' => $param['name'],
            'uname' => $param['uname'],
            'open' => 'Y',
            'image_id' => $param['image_id'],
            'page_title' => $param['page_title'],
            'page_keyword' => $param['page_keyword'],
            'page_description' => $param['page_description'],
            'description' => $param['description'],
            'mob_description' => $param['mob_description'],
            'author_id' => $param['author_id'],
            'type' => $param['type'],
            'use_volume' => $param['use_volume'],
            'status' => $param['status']
        );
        
        $this->db->where('id', $param['id']);
        $this->db->update('book', $data);
    }

    public function deteleBook($id){
        $this->db->where('id', $id);
        $this->db->delete('book');
    }

    public function getBookByUname($book_uname){
        $this->db->select(' book.id as book_id, book.name as book_name, book.uname as book_uname, book.image_id as book_image, book.author_id as book_author, page_title, page_keyword, 
                            page_description, description, mob_description, book.type as book_type, use_volume, status,
                            author.name as author_name, filename, category_book.book_id as category_book_book_id,
                            category_book.category_id as category_book_category_id
                         ');
        $this->db->from('book');
        $this->db->join('author'        , 'book.author_id = author.id', 'inner');
        $this->db->join('category_book'        , 'category_book.book_id = book.id', 'inner');
        $this->db->join('image'         , 'book.image_id = image.id', 'left');
        $this->db->where("book.uname", $book_uname);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function getBooksByCategoryID($category_id){
        $this->db->select(' uname, category_id');
        $this->db->from('book');
        $this->db->join('category_book' , 'book.id = category_book.book_id', 'inner');
        $this->db->where('category_book.category_id'   , $category_id);
        $query = $this->db->get();
        return $query->result_array();
    } 
    
	public function getBookNameByBookID($book_id){
        $this->db->select(' name');
        $this->db->from('book');
        $this->db->where('id'   , $book_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function countBook(){
        $this->db->select('count(book.id) as total');
        $this->db->from('book');       
        $query = $this->db->get();
        return $query->row('total');
    }
}
?>